/*
	新增新闻
*/
var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');
var dictData = Cookies.getJSON('kplus_web_dict');

//dom
$(function(){
	layui.use(['layer','form'], function(){
		var layer = layui.layer;
		var form = layui.form;
		//富文本
		var WEditor = window.wangEditor,
            editorCn = new WEditor('#htmlCn'),
            editorTw = new WEditor('#htmlTw'),
            editorEn = new WEditor('#htmlEn');
        editorCn.create();
        editorTw.create();
        editorEn.create();
        // ajax data
        var ajaxData = {
        	aId: adminLoginedInfo.aId,
        	aToken: adminLoginedInfo.aToken
        };
        //确定新增
        form.on('submit(newAdd)', function(data){
        	console.log(data.field);
        	var field = data.field;
        	layer.confirm(MESSAGES.confirm_add, {title:'提示'}, function(index){
        		var new_img_num = $('#new-banner-box .layui-form-item').length;
        		var form_data = {
        			title: langTransObj(field.titleCn, field.titleTw, field.titleEn),
        			cn: editorCn.txt.html(),
                	tw: editorTw.txt.html(),
                	en: editorEn.txt.html(),
                	tags: field.tags, 
                	status: field.status
        		}
        		var img_arr = [];
	        	for(var i = 0; i < new_img_num; i++){
	        		img_arr.push(data.field['newsImg'+i]);
	        	}
	        	form_data['img'] = img_arr.join('|');
	        	//新增算法
	        	newsAdd(form_data);
        		layer.close(index);
        	})
        	return false;
        });
        //重置表单
		$('[lay-filter="reset"]').on('click', function(){
		    layer.confirm(MESSAGES.reset_form, { btn:['确认','取消'] }, function(index){  //确认
		        location.reload();
		        layer.close(index);
		    });
		});
        //新增算法
        function newsAdd(news_data){
        	var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
        	var _ajaxData = $.extend({}, news_data, ajaxData);
        	$.ajax({
        		url: BASE_SERVICEURL + URlS.news,
        		type: 'PUT',
        		data: _ajaxData,
        		success: function(responseData){
        			if(responseData.msg.code == 200){
        				layer.close(loadIndex);
        				layer.msg(MESSAGES.new_add_success, {time: 2000, end: function(){
        					//关闭当前窗口并刷新父窗口
							parent.layer.close(layer.index);
                        	window.parent.location.reload();
        				}})
        			}else{
        				layer.close(loadIndex);
        				layer.msg(responseData.msg.msg, {time: 2000});
        			}
        		},
        		error: function(responseData){
        			layer.close(loadIndex);
        			layer.msg(MESSAGES.service_error, {time: 2000, end: function(){
        				location.href = './500.html';
        			}})
        		} 
        	})
        }





	});
})











